package com.example.mywanandroid.ui.nav.qa;

import android.os.Bundle;
import android.view.View;

import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.fragment.NavHostFragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.mywanandroid.BR;
import com.example.mywanandroid.R;
import com.example.mywanandroid.base.BaseFragment;
import com.example.mywanandroid.bean.responsebean.ArticleBean;
import com.example.mywanandroid.config.Constants;
import com.example.mywanandroid.databinding.FragmentListBinding;
import com.example.mywanandroid.ui.activity.login.LoginActivity;
import com.example.mywanandroid.ui.activity.web.DetailsActivity;
import com.example.mywanandroid.ui.adapter.CommonAdapter;
import com.example.mywanandroid.ui.nav.square.SquareFragment;
import com.orhanobut.hawk.Hawk;

public class QaFragment extends BaseFragment<FragmentListBinding,QaViewModel> {

    private CommonAdapter<ArticleBean> commonAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_list;
    }

    @Override
    protected void initViewModel() {
        mViewModel = new ViewModelProvider(this).get(QaViewModel.class);
    }

    @Override
    protected void bindViewModel() {

    }

    @Override
    protected void init() {
        mViewModel.loadData();

        addObserver();

        initRefreshLayout();
        initRecycleView();
    }

    private void addObserver() {
        mViewModel.getArticleList().observe(this,articleListBean -> {
            if (articleListBean.getCurPage() >= articleListBean.getPageCount()) {
                mDataBinding.refreshLayout.finishLoadMoreWithNoMoreData();
            }
            mDataBinding.refreshLayout.finishRefresh();
            mDataBinding.refreshLayout.finishLoadMore();

            commonAdapter.onItemDatasChanged(articleListBean.getDatas());
        });
    }

    private void initRefreshLayout() {
        mDataBinding.refreshLayout.setOnRefreshListener(refreshLayout -> mViewModel.refreshData(true));
        mDataBinding.refreshLayout.setOnLoadMoreListener(refreshLayout -> mViewModel.refreshData(false));
    }

    private void initRecycleView() {

        commonAdapter = new CommonAdapter<ArticleBean>(R.layout.item_article, com.example.mywanandroid.BR.articleBean) {
            @Override
            public void addListener(View root, ArticleBean itemData, int position) {

                root.findViewById(R.id.card_view).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DetailsActivity.start(getActivity(), itemData.getLink());
                    }
                });

                root.findViewById(R.id.iv_collect).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (Hawk.get(Constants.HawkCode.LOGIN_DATA) != null) {
                            itemData.setCollect(!itemData.isCollect());
                            notifyDataSetChanged();
                            mViewModel.changeArticleCollect(itemData.isCollect(), itemData.getId());
                        } else {
                            LoginActivity.start(getContext());
                        }
                    }
                });

                root.findViewById(R.id.tv_author).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Bundle bundle = new Bundle();
                        bundle.putInt(Constants.ParamCode.PARAM1, itemData.getUserId());
                        NavHostFragment.findNavController(QaFragment.this).navigate(R.id.nav_share, bundle);
                    }
                });
            }
        };

        mDataBinding.recycle.setLayoutManager(new LinearLayoutManager(getContext()));
        mDataBinding.recycle.setAdapter(commonAdapter);
    }
}
